package com.um.jdy.platform.order.manager;


import com.um.jdy.common.charge.service.OrderCommissionService;
import com.um.jdy.common.system.service.ParamService;
import com.um.jdy.platform.order.entity.dto.CommissionSummaryItemVO;
import com.um.jdy.platform.order.entity.dto.CommissionSummaryPageDTO;
import com.um.jdy.platform.order.entity.dto.CommissionSummaryVO;
import com.um.jdy.platform.order.entity.dto.ExportOrderSummaryItemVO;
import com.um.jdy.platform.order.entity.vo.OrderSummaryItemVO;
import com.um.jdy.platform.order.entity.vo.OrderSummaryVO;
import com.um.jdy.platform.order.entity.vo.StationSummaryItemVO;
import com.um.jdy.platform.order.entity.vo.StationSummaryVO;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.ExportParams;
import com.um.springboot.starter.utils.ExcelUtil;
import com.um.springboot.starter.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 管理类：订单佣金表
 */
@Component
public class OrderSummaryManager extends BaseManager<OrderCommissionService> {

    @Autowired
    ParamService paramService;

    /*************************************订单统计*************************************/
    /**
     * 站场订单统计
     * @param pageNo
     * @param pageSize
     * @param name
     * @param yearOrMonth
     * @return
     */
    public Page<OrderSummaryItemVO> getStationOrderSummaryPage(int pageNo, int pageSize, String name, String yearOrMonth) {
        Page<OrderSummaryItemVO> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        String beginDate = "";
        String endDate = "";
        String[] arr = org.apache.commons.lang3.StringUtils.split(yearOrMonth, "-");
        if(arr.length > 1) {
            int days = com.um.springboot.starter.utils.DateUtil.checkMonth(StringUtil.toInt(arr[1]), StringUtil.toInt(arr[0]));

            beginDate = yearOrMonth + "-01 00:00:00";
            endDate = yearOrMonth + "-" + (days < 10 ? "0" + days : days) + " 23:59:59";
        }else{
            beginDate = yearOrMonth + "-01-01 00:00:00";
            endDate = yearOrMonth + "-12-30 23:59:59";
        }

        page = service.getViewModelPageBySqlName(page, "get_station_order_summary_page", "get_station_order_summary_count", OrderSummaryItemVO.class,new Object[]{beginDate,endDate,getComId(),name});
        OrderSummaryVO summary = service.getViewModel("get_station_order_summary", OrderSummaryVO.class,new Object[]{beginDate,endDate,getComId(),name});
        page.setExtraData(summary);
        return page;
    }

    public Response exportStationOrderSummaryPage(ExportParams exportParams, String yearOrMonth, String name) {

        String beginDate = "";
        String endDate = "";
        String[] arr = org.apache.commons.lang3.StringUtils.split(yearOrMonth, "-");
        if(arr.length > 1) {
            int days = com.um.springboot.starter.utils.DateUtil.checkMonth(StringUtil.toInt(arr[1]), StringUtil.toInt(arr[0]));

            beginDate = yearOrMonth + "-01 00:00:00";
            endDate = yearOrMonth + "-" + (days < 10 ? "0" + days : days) + " 23:59:59";
        }else{
            beginDate = yearOrMonth + "-01-01 00:00:00";
            endDate = yearOrMonth + "-12-30 23:59:59";
        }

        List<ExportOrderSummaryItemVO> list = service.getCMSViewModelListBySqlName("get_station_order_summary_page", ExportOrderSummaryItemVO.class, exportParams.getProperties(),
                exportParams.getValues(),exportParams.getRangeEntitys(),exportParams.getOrderNames(),exportParams.getSortOns(),
                new Object[]{beginDate, endDate, getComId(),name, exportParams.getStart(),exportParams.getLimit()});

        list.stream().forEach(item ->{
            item.setStationAddress(item.getProvince()+item.getCity()+item.getArea()+item.getAddress());
        });


        Map mapFormat = new Hashtable();
        mapFormat.put("order_amount", "number");
        mapFormat.put("place_amount", "number");
        mapFormat.put("amount", "number");


        String fileName = "Excel"+ com.um.springboot.starter.utils.DateUtil.getDateString(new Date(), "yyyyMMddHHmmss")+".xls";
        String path = service.getExportFilePath(fileName);

        List<String> columns = Arrays.asList("网点名称", "网点地址", "运营商", "手机号码", "订单数", "金额");
        List<String> properties = Arrays.asList("station_name", "station_address", "merchant_name", "merchant_mobile", "order_num", "order_amount");

        ExcelUtil.writeEntity(path, columns, list, properties, exportParams.getMapParam(), mapFormat, exportParams.getMapZero(),true);

        return Response.success(service.getDownFilePath(fileName));
    }

    /**
     * 站场订单明细统计
     * @param stationId
     * @param yearOrMonth
     * @return
     */
    public StationSummaryVO getStationOrderDetailList(String stationId, String type, String yearOrMonth) {

        List<StationSummaryItemVO> stationList = new ArrayList<>();
        int totalOrderNum = 0;
        int totalOrderAmount = 0;
        int totalPlaceNum = 0;
        int totalPlaceAmount = 0;

        if (type.equals("year")) {

            String beginDate = yearOrMonth+"-01-01 00:00:00";
            String endDate = yearOrMonth+"-12-30 23:59:59";

            List<StationSummaryItemVO> orders = service.getViewModelList("get_station_order_detail_list_by_year", StationSummaryItemVO.class,new Object[]{beginDate,endDate, stationId,getComId()});

            for (int index = 1; index <= 12; index++) {
                String month = yearOrMonth.trim() + "-" + (index < 10 ? "0" + index : index);

                StationSummaryItemVO item = new StationSummaryItemVO();
                item.setMonthOrDate(month);

                for (StationSummaryItemVO orderItem : orders) {
                    if (orderItem.getMonthOrDate().equals(month)) {
                        item.setOrderNum(orderItem.getOrderNum());
                        item.setOrderAmount(orderItem.getOrderAmount());
                        totalOrderNum += orderItem.getOrderNum();
                        totalOrderAmount += orderItem.getOrderAmount();
                        break;
                    }
                }
                stationList.add(item);
            }
        } else if (type.equals("month")) {
            String[] arr = org.apache.commons.lang3.StringUtils.split(yearOrMonth, "-");
            int days = com.um.springboot.starter.utils.DateUtil.checkMonth(StringUtil.toInt(arr[1]), StringUtil.toInt(arr[0]));

            String beginDate = yearOrMonth+"-01 00:00:00";
            String endDate = yearOrMonth+"-"+(days < 10 ?"0"+days:days)+" 23:59:59";
            List<StationSummaryItemVO> orders = service.getViewModelList("get_station_order_detail_list_by_month", StationSummaryItemVO.class,new Object[]{beginDate,endDate, stationId,getComId()});

            for (int index = 1; index <= days; index++) {
                String date = yearOrMonth.trim() + "-" + (index < 10 ? "0" + index : index);

                StationSummaryItemVO item = new StationSummaryItemVO();
                item.setMonthOrDate(date);

                for (StationSummaryItemVO orderItem : orders) {
                    if (orderItem.getMonthOrDate().equals(date)) {
                        item.setOrderNum(orderItem.getOrderNum());
                        item.setOrderAmount(orderItem.getOrderAmount());
                        totalOrderNum += orderItem.getOrderNum();
                        totalOrderAmount += orderItem.getOrderAmount();
                        break;
                    }
                }

                stationList.add(item);
            }
        }

        StationSummaryVO summaryVO = new StationSummaryVO();
        summaryVO.setOrderNum(totalOrderNum);
        summaryVO.setOrderAmount(totalOrderAmount);
        summaryVO.setPlaceNum(totalPlaceNum);
        summaryVO.setPlaceAmount(totalPlaceAmount);
        summaryVO.setAmount(totalOrderAmount + totalPlaceAmount);
        summaryVO.setList(stationList);
        return summaryVO;
    }


    /**
     * 分页查询运营商佣金统计
     * @param params
     * @return
     */
    public Response<Page<CommissionSummaryItemVO>> getSummaryPage(CommissionSummaryPageDTO params){
        Page<CommissionSummaryItemVO> page = new Page<>();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());

        Object[] values = new Object[]{params.getBeginTime(),params.getEndTime(),params.getBeginTime(),params.getEndTime(),getComId(),params.getName(),params.getType()};

        page = service.getViewModelPageBySqlName(page,"get_commission_summary_page","get_commission_summary_count", CommissionSummaryItemVO.class, values);
        CommissionSummaryVO summaryVO = service.getCMSViewModelBySqlName("get_commission_summary", CommissionSummaryVO.class,new String[]{},new Object[]{},new ArrayList<>(),values);

        page.setExtraData(summaryVO);
        return Response.success(page);
    }

}
